<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% js_bundle :select_content_dialog %>
<% css_bundle :select_content_dialog %>
<% only ||= nil # only is an optimization, so that tabs that aren't going to be used won't have their data loaded %>
<% lti_app_placements ||= [] %>
<% cache([@context, 'select_context_content_dialog', only].cache_key) do %>
  <div id="select_context_content_dialog" style="display: none;">
    <div style="margin-bottom: 5px; <%= hidden if only && only.size == 1 %>">
      <a href="<%= context_url(@context, :context_external_tool_resource_selection_url, "{{ id }}") %>" id="select_content_resource_selection_url" style="display: none;">&nbsp;</a>
      <% ot :add_to_module, "Add %{item} to *module*", :item => capture { %>
      <select style="margin-bottom: 0px" id="add_module_item_select" aria-label="<%= t 'select_item_type_label', "Select the type of content to add." %>">
        <% if !only || only.include?(:assignments) %>
        <option value="assignment"><%= t :assignment_item, "Assignment" %></option>
        <% end %>
        <% if !only || only.include?(:quizzes) %>
        <option value="quiz"><%= t :quiz_item, "Quiz" %></option>
        <% end %>
        <% if !only || only.include?(:attachments) %>
        <option value="attachment"><%= t :file_item, "File" %></option>
        <% end %>
        <% if !only || only.include?(:wiki_pages) %>
        <option value="wiki_page"><%= t :content_page_item, "Content Page" %></option>
        <% end %>
        <% if !only || only.include?(:discussion_topics) %>
        <option value="discussion_topic"><%= t :discussion_item, "Discussion" %></option>
        <% end %>
        <% if !only || only.include?(:sub_headers) %>
        <option value="context_module_sub_header" class="context_module_content"><%= t :text_header_item, "Text Header" %></option>
        <% end %>
        <% if !only || only.include?(:external_urls) %>
        <option value="external_url" class="context_module_content"><%= t :external_url_item, "External URL" %></option>
        <% end %>
        <% if !only || only.include?(:external_tools) %>
        <option value="context_external_tool" class="context_module_content"><%= t :external_tool_item, "External Tool" %></option>
        <% end %>
      </select>
      <% }, :wrapper => '<span class="holder_name">\1</span>' %>
    </div>
      <% if !only || only.include?(:sub_headers) %>
      <div id="context_module_sub_headers_select" class="module_item_option">
        <div style="font-size: 0.8em;">
          <%= t :sub_header_notice, "Type the text you'd like to add as a *module* sub-header", :wrapper => '<span class="holder_type">\1</span>' %>
        </div>
        <table>
          <tr>
            <td><label for="sub_header_title"><%= before_label :header, "Header" %></label></td>
            <td><input type="text" name="title" id="sub_header_title" style="width: 200px;"/></td>
          </tr>
        </table>
      </div>
      <% end %>
      <% if !only || only.include?(:external_urls) %>
      <div id="external_urls_select" class="module_item_option">
        <div style="font-size: 0.8em;">
          <i class="icon-link"></i>
          <%= t :external_url_notice, "Enter a URL and page name to add a link to any website URL to this *module*.", :wrapper => '<span class="holder_type">\1</span>' %>
        </div>
        <table>
          <tr>
            <td><label for="content_tag_create_url"><%= before_label :url, "URL" %></label></td>
            <td>
              <input type="text" name="url" id="content_tag_create_url" style="width: 200px;"/>
              <div id="content_tag_services">

              </div>
            </td>
          </tr>
          <tr class='select_item_name'>
            <td><label for="content_tag_create_title"><%= before_label :page_name, "Page Name" %></label></td>
            <td><input type="text" name="title" id="content_tag_create_title" style="width: 120px;"/></td>
          </tr>
          <tr>
            <td colspan="2">
              <input type="checkbox" name="new_tab" id="external_url_create_new_tab"/>
              <label for="external_url_create_new_tab"><%= t :new_tab, "Load in a new tab" %></label>
            </td>
          </tr>
        </table>
      </div>
      <% end %>
      <% if !only || only.include?(:external_tools) %>
      <div id="context_external_tools_select" class="module_item_option">
        <div style="font-size: 0.8em;" tabindex="0" role="alert">
          <i class="icon-link"></i>
          <%= t :external_tool_notice, "Select a tool from the list below, or enter a URL for an external tool you already know is configured with Basic LTI to add a link to it to this *module*.", :wrapper => '<span class="holder_type">\1</span>' %>
        </div>
        <a href="<%= context_url(@context, controller:'lti/lti_apps', action:'launch_definitions', :placements => lti_app_placements, :per_page => '100') %>" class="external_tools_url" style="display: none;">&nbsp;</a>
        <table>
          <tr>
            <td colspan="2">
              <ul class="tools" style="max-height: 250px; overflow: auto;">
                <li class="message" style="margin: 10px 0;"><%= t :loading, "Loading..." %></li>
                <li class="tool" style="display: none;">
                  <a href="#" class="name"></a>
                  <div class="description"></div>
                </li>
              </ul>
            </td>
          </tr>
          <tr>
            <td><label for="external_tool_create_url"><%= before_label :url, "URL" %></label></td>
            <td>
              <input type="text" name="url" id="external_tool_create_url" style="width: 200px;"/>
              <div class="domain_message" style="font-size: 0.8em; display: none;">
                <%= t :domain_match_notice, "to use this tool the domain must match %{domain}", :domain => '<span class="domain">&nbsp;</span>'.html_safe %>
              </div>
            </td>
          </tr>
          <tr class='select_item_name'>
            <td><label for="external_tool_create_title"><%= before_label :page_name, "Page Name" %></label></td>
            <td><input type="text" name="title" id="external_tool_create_title" style="width: 120px;"/></td>
          </tr>
          <tr>
              <td colspan="2">
              <input type="checkbox" name="new_tab" id="external_tool_create_new_tab"/>
              <label for="external_tool_create_new_tab"><%= t :new_tab, "Load in a new tab" %></label>
            </td>
          </tr>
        </table>
      </div>
      <% end %>
      <% if !only || only.include?(:wiki_pages) %>
      <div id="wiki_pages_select" class="module_item_option">
        <div style="font-size: 0.8em;">
          <i class="icon-document"></i>
          <%= t 'Select the page you want to associate with this *module*, or add a new page by selecting "New Page".', :wrapper => '<span class="holder_type">\1</span>' %>
        </div>
        <div style="margin-top: 5px;">
        <select class="module_item_select" aria-label="<%= t 'Select the page you want to associate, or add a new page by selecting "New Page".' %>" multiple>
          <%# cache on the context.wiki instead of just the context because even though it will require a db query, we will get a much better cache hit rate %>
          <option value="new"><%= t :new_page_option, "[ New Page ]" %></option>
          <% cache([@context.wiki, 'non_deleted_wiki_pages']) do  %>
            <% @context.wiki_pages.not_deleted.select([:id, :title]).each do |page| %>
              <% if page && !page.new_record? %>
                <option value="<%= page.id %>"><%= page.title %></option>
              <% end %>
            <% end %>
          <% end %>
        </select>
        </div>
        <div class="new" style="margin-top: 5px;">
          <%= before_label :wiki_page_name, "Page Name" %>
          <input type="text" name="wiki_page[title]" class="item_title" aria-label="<%= before_label :page_name, "Page Name" %>"/>
          <a href="<%= polymorphic_url([:api_v1, @context, :wiki_pages]) %>" style="display: none;" class="add_item_url">&nbsp;</a>
        </div>
      </div>
      <% end %>
      <% if !only || only.include?(:attachments) %>
      <div id="attachments_select" class="module_item_option">
        <div style="font-size: 0.8em;" id="file_select_label">
          <i class="icon-download"></i>
          <%= t :file_notice, 'Select the file you want to associate with this *module*, or add a file by selecting "New File".', :wrapper => '<span class="holder_type">\1</span>' %>
        </div>
        <div style="margin-top: 5px;">
          <div id="module_item_select_file">
          </div>
        </div>
        <div class="new" style="margin-top: 5px;">
          <table>
            <tr>
              <td><%= before_label :file, "File" %></td>
              <td><input id="module_attachment_uploaded_data" type="file" name="attachment[uploaded_data]" class="item_title"/></td>
            </tr><tr>
              <td>
                <a href="<%= context_url(@context, :context_files_url, :format => :text) %>" style="display: none;" class="add_item_url">&nbsp;</a>
                <%= before_label :folder, "Folder" %>
              </td><td>
                <% Folder.root_folders(@context) %>
                <% folders = @context.active_folders.select([:id, :name, :parent_folder_id]).to_a %>
                <% root_folders = folders.select{|f| f.parent_folder_id == nil} %>
                <select name="attachment[folder_id]" id="attachment_folder_id">
                  <%= folders_as_options(root_folders, :all_folders => folders, :max_depth => 1) %>
                </select>
              </td>
            </tr>
          </table>
        </div>
      </div>
      <% end %>
      <% if !only || only.include?(:quizzes) %>
      <div id="quizs_select" class="module_item_option">
        <div style="font-size: 0.8em;">
          <i class="icon-quiz"></i>
          <%= t :quiz_notice, 'Select the quiz you want to associate with this *module*, or add a new one by selecting "New Quiz".', :wrapper => '<span class="holder_type">\1</span>' %>
        </div>
        <div style="margin-top: 5px;">
        <select class="module_item_select" aria-label="<%= t :quiz_select_label, 'Select the quiz you want to associate, or add a new one by selecting "New Quiz".' %>" multiple>
          <% if @context.grants_right?(@current_user, session, :manage_assignments) %>
            <option value="new"><%= t :new_quiz_option, "[ New Quiz ]" %></option>
          <% end %>
          <% @context.quizzes.active.limit(400).each do |quiz| %>
            <option value="<%= quiz.id %>"><%= quiz.title %></option>
          <% end %>
        </select>
        </div>
        <div class="new" style="margin-top: 5px;">
          <input type="hidden" name="quiz[quiz_type]" value="assignment" />
          <table>
            <tr>
              <td><%= before_label :quiz_name, "Quiz Name" %></td>
              <td><input type="text" name="quiz[title]" class="item_title" aria-label="<%= before_label :quiz_name, "Quiz Name" %>"/></td>
            </tr><tr>
              <td><%= before_label :quiz_group, "Group" %></td>
              <td>
                <select name="quiz[assignment_group_id]" aria-label="<%= before_label :quiz_group, "Group" %>">
                  <% cnt = 0 %>
                  <% @context.assignment_groups.active.each do |group| %>
                    <% cnt += 1 %>
                    <option value="<%= group.id %>"><%= group.name %></option>
                  <% end %>
                  <% if cnt == 0 %>
                    <option value=""><%= t 'options.assignments', "Assignments" %></option>
                  <% end %>
                </select>
              </td>
            </tr>
          </table>
          <a href="<%= context_url(@context, :context_quizzes_url) %>" style="display: none;" class="add_item_url">&nbsp;</a>
        </div>
      </div>
      <% end %>
      <% if !only || only.include?(:assignments) %>
      <div id="assignments_select" class="module_item_option">
        <div style="font-size: 0.8em;">
          <i class="icon-assignment"></i>
          <%= t :assignment_notice, 'Select the assignment you want to associate with this *module*, or add an assignment by selecting "New Assignment".', :wrapper => '<span class="holder_type">\1</span>' %>
        </div>
        <div style="margin-top: 5px;">
        <select class="module_item_select" aria-label="<%= t 'assignment_item_select_label', 'Select the assignment you want to associate, or add an assignment by selecting "New Assignment".' %>" multiple>
          <% if @context.grants_right?(@current_user, session, :manage_assignments) %>
            <option value="new"><%= t :new_assignment_option, "[ New Assignment ]" %></option>
          <% end %>
          <% @context.assignment_groups.active.include_active_assignments.each do |group| %>
            <optgroup label="<%= group.name %>">
            <% group.active_assignments.no_submittables.limit(200).each do |assignment| %>
              <option value="<%= assignment.id %>"><%= assignment.title %></option>
            <% end %>
            </optgroup>
          <% end %>
        </select>
        </div>
        <div class="new" style="margin-top: 5px;">
          <%= before_label :wiki_page_namem, "Assignment Name" %>
          <input type="text" name="assignment[title]" class="item_title" style="margin-bottom: 5px;" aria-label="<%= before_label :assignment_name, "Assignment Name" %>"/>
          <a href="<%= context_url(@context, :context_assignments_url) %>" style="display: none;" class="add_item_url">&nbsp;</a>
        </div>
      </div>
      <% end %>
      <% if !only || only.include?(:discussion_topics) %>
      <div id="discussion_topics_select" class="module_item_option">
        <div style="font-size: 0.8em;">
          <i class="icon-discussion"></i>
          <%= t :topic_notice, 'Select the topic you want to associate with this *module*, or add a topic by selecting "New Topic".', :wrapper => '<span class="holder_type">\1</span>' %>
        </div>
        <div style="margin-top: 5px;">
        <select class="module_item_select" aria-label="<%= t 'discussion_topic_select_label', 'Select the topic you want to associate, or add a topic by selecting "New Topic".' %>" multiple>
          <option value="new"><%= t :new_topic_option, "[ New Topic ]" %></option>
          <% @context.discussion_topics.active.only_discussion_topics.limit(200).each do |topic| %>
            <option value="<%= topic.id %>"><%= topic.title %></option>
          <% end %>
        </select>
        </div>
        <div class="new" style="margin-top: 5px;">
          <%= before_label :topic_name, "Topic Name" %>
          <input type="text" name="title" class="item_title" aria-label="<%= before_label :topic_name, "Topic Name" %>" />
          <input type="hidden" name="published" value="false"/>
          <a href="<%= context_url(@context, :api_v1_context_discussion_topics_url) %>" style="display: none;" class="add_item_url">&nbsp;</a>
        </div>
      </div>
      <% end %>
      <% if !only || only.include?(:indentation) %>
      <div class="context_module_content">
        <%= before_label :indentation, "Indentation" %>
        <select name="content_tag[indent]" id="content_tag_indent" aria-label="<%= before_label :indentation, "Indentation" %>">
          <% (0..3).each do |level| %>
            <%= t(:indent_level, { :zero => "*Don't Indent*", :one => "*Indent 1 Level*", :other => "*Indent %{count} levels*" }, :count => level, :wrapper => "<option value=\"#{level}\">\\1</option>") %>
          <% end %>
        </select>
      </div>
      <% end %>
    <div class="button-container">
      <button type="button" class="cancel_button btn"><%= t "#buttons.cancel", "Cancel" %></button>
      <button type="button" class="add_item_button btn btn-primary"><%= t "#buttons.add_item", "Add Item" %></button>
    </div>
  </div>
<% end %>
